package com.system.healthproject.model.admin.service.impl;

import com.system.healthproject.bean.AppCodeBean;
import com.system.healthproject.bean.AppCodeResultBean;
import com.system.healthproject.bean.AppCodeUserBean;
import com.system.healthproject.bean.PagesBean;
import com.system.healthproject.mapper.AppCodeMapper;
import com.system.healthproject.model.admin.paramsbean.AppCodeParams;
import com.system.healthproject.model.admin.service.AppCodeService;
import com.system.healthproject.pojo.AppCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class AppCodeServiceImp implements AppCodeService {

    @Autowired
    AppCodeMapper appCodeMapper;



    @Transactional
    @Override
    public boolean insertAppCode(AppCodeParams appCode) {
        AppCode record = new AppCode();
        record.setProjectName(appCode.getProjectName());
        record.setHospitalId(appCode.getHospitaId());
        record.setAppcodeNumber(appCode.getAppCodeNumber());
        record.setAppcodeCreatetime(new Date());
        record.setAppcodeRemainder(appCode.getDoctorReward());
        record.setMembershiptime(appCode.getMembershipTime());
        record.setDoctorreward(appCode.getDoctorReward());
        int insert = appCodeMapper.insert(record);
        Integer appcodeId = record.getAppcodeId();
        List<Integer> doctorIds = appCode.getDoctorIds();
        appCodeMapper.addCodeDoctor(appcodeId, doctorIds);
        return insert==1;
    }

    @Override
    public PagesBean<AppCodeResultBean> selectAppCodeWhere(
            Integer page,
            Integer size,
            String projectName,
            String hospitalName,
            String appcodeCreatetime) {
        PagesBean<AppCodeResultBean> pagesBean=new PagesBean<>();

        List<AppCodeResultBean> appCodeResultBeans = appCodeMapper
                .selectAppCodeWhere((page-1)*size, size, projectName, hospitalName, appcodeCreatetime);

        int i = appCodeMapper.selectTotal(projectName, hospitalName, appcodeCreatetime);
        pagesBean.setData(appCodeResultBeans);
        pagesBean.setNowsPage(page);
        pagesBean.setPageSize(size);
        pagesBean.setDataCount(i);
        return pagesBean;
    }

    @Override
    public AppCodeBean selectOneAppCode(Integer appcodeId) {
        AppCodeBean infoById = appCodeMapper.getInfoById(appcodeId);
        return infoById;
    }

    @Override
    public PagesBean<AppCodeUserBean> selectUsePatientbyCode(
            Integer appcodeId,
            Integer page,
            Integer size) {
        PagesBean<AppCodeUserBean> pagesBean=new PagesBean<>();
        List<AppCodeUserBean> appCodeUserBeans =
                appCodeMapper.selectUserCodeById(appcodeId, (page - 1) * size, size);
        Integer total = appCodeMapper.selectUserCodeTotal(appcodeId);
        pagesBean.setData(appCodeUserBeans);
        pagesBean.setNowsPage(page);
        pagesBean.setPageSize(size);
        pagesBean.setDataCount(total);
        return pagesBean;
    }

    @Override
    public boolean deleteAppCode(Integer appCodeId) {
        Integer integer = appCodeMapper.deleteCodeAndDoctor(appCodeId);
        boolean b = appCodeMapper.deleteByPrimaryKey(appCodeId) == 1;
        return b;
    }
}
